<?php
/**
 * @package mikrokosmos
 * mk script file created on 10-giu-2005
 * jaco_at_pixeldump_dot_org
 *
 */

//------------------------------------------------------------------------
// "READ" FUNCTIONS
//------------------------------------------------------------------------

/**
 * reads data
 * fill an associative array
 * with defineText/2 tag fields
 */
function read_defineText_data($tagData){

	$dtd = array();
	$dtd = read_tag_header_data(substr($tagData, 0, 10));
	$ofst = $dtd["tagSL"] + 2;

	$dtd["textBounds"] = read_rect_data(substr($tagData, $ofst, MAX_RECT_LENGTH));

	$ofst += $dtd["textBounds"]["rectLength"];

	$dtd["textMatrix"] = read_matrix_data(substr($tagData, $ofst));

	$ofst += $dtd["textMatrix"]["matrixLength"];

	$dtd["glyphBits"] = ui8dec($tagData[$ofst]);

	$ofst++;
	$dtd["advanceBits"] = ui8dec($tagData[$ofst]);

	$ofst++;
	$dtd["textRecords"] = array();

	//$trBuf = substr($tagData, $ofst);
	$tdLength = strlen($tagData);

	//while($ofst < $tdLength){

	//}

	$dtd["endOfRecordsFlag"] = 0;

	return $dtd;
}

/**
 * reads data
 * fill an associative array with TEXTRECORD fields
 * from a given binary data
 */
function read_textRecord_data($buf){

	$trd = array();

	return $trd;
}


/**
 * reads data
 * fill an associative array with GLIPHENTRY fields
 * from a given binary data
 */
function read_gliphEntry_data($buf){

	$ged = array();

	$ged["gliphIndex"]   = ui8dec($buf[0]);
	$ged["gliphAdvance"] = si8dec($buf[1]);

	return $ged;
}

/**
 * reads data
 * fill an associative array
 * with defineEditText tag fields
 */
function read_textField_data ($tagData){

	$detd = read_tag_header_data(substr($tagData, 0, 10));
	$ofst = $detd["tagSL"] + 2;

	$detd["bounds"] = read_rect_data(substr($tagData, $ofst, MAX_RECT_LENGTH));
	$ofst += $detd["bounds"]["rectLength"];

	$fo = get_binData_string(substr($tagData, $ofst, 2));
	$ofst += 2;

	$fd = array();
	for($i = 0; $i < 16; $i++)
		$fd[$i] = $fo[$i] == "1" ? 1 : 0;

	$detd["hasText"]	  = $fd[0];
	$detd["wordWrap"]	  = $fd[1];
	$detd["multiline"]	  = $fd[2];
	$detd["password"]	  = $fd[3];
	$detd["readOnly"]	  = $fd[4];
	$detd["hasTextColor"] = $fd[5];
	$detd["hasMaxLength"] = $fd[6];
	$detd["hasFont"]	  = $fd[7];

	$detd["hasFontClass"] = $fd[8];
	$detd["autoSize"]	  = $fd[9];
	$detd["hasLayout"]	  = $fd[10];
	$detd["noSelect"]	  = $fd[11];
	$detd["border"]		  = $fd[12];
	$detd["wasStatic"]	  = $fd[13];
	$detd["html"]		  = $fd[14];
	$detd["useOutlines"]  = $fd[15];


	if($detd["hasFont"]){
		$detd["fontID"]	    = ui16dec(substr($tagData, $ofst, 2));
		$ofst += 2;
		$detd["fontHeight"] = ui16dec(substr($tagData, $ofst, 2));
		$ofst += 2;
	}

	if($detd["hasFontClass"]){
		$detd["fontClass"] = read_swf_string(substr($tagData, $ofst));
		$ofst += strlen($detd["fontClass"]) + 1;
	}

	if($detd["hasTextColor"]){

		$detd["textColor"] = read_color_data(substr($tagData, $ofst, 4), SHAPE3);
		$ofst += 4;
	}

	if($detd["hasMaxLength"]){
		$detd["maxLength"] = ui16dec(substr($tagData, $ofst, 2));
		$ofst += 2;
	}

	if($detd["hasLayout"]){

		$detd["align"] = ui8dec(substr($tagData, $ofst, 2));
		$ofst ++;
		$detd["leftMargin"] = ui16dec(substr($tagData, $ofst, 2));
		$ofst += 2;
		$detd["rightMargin"] = ui16dec(substr($tagData, $ofst, 2));
		$ofst += 2;
		$detd["indent"] = ui16dec(substr($tagData, $ofst, 2));
		$ofst += 2;
		$detd["leading"] = ui16dec(substr($tagData, $ofst, 2));
		$ofst += 2;
	}

	$detd["variableName"] = read_swf_string(substr($tagData, $ofst));
	$ofst += strlen($detd["variableName"]) + 1;

	if($detd["hasText"]){
		$detd["initialText"] = read_swf_string(substr($tagData, $ofst));
	}

	return $detd;
}

/**
 * reads data
 * fill an associative array with defineFont2/3 fields
 */
function read_defineFont_data($tagData){

	$dfd = read_tag_header_data(substr($tagData, 0, 10));
	$ofst = $dfd["tagSL"] + 2;

	$fStr = get_binData_string($tagData[$ofst]);
	$ff = array();

	for($i = 0; $i < 8; $i++) $ff[$i] = $fStr[$i] == "1" ? 1 : 0;

	$dfd["fontFlagsHasLayout"]	 = $ff[0];
	$dfd["fontFlagsShiftJIS"]	 = $ff[1];
	$dfd["fontFlagsSmallText"]	 = $ff[2];
	$dfd["fontFlagsANSI"]		 = $ff[2];
	$dfd["fontFlagsWideOffsets"] = $ff[4];
	$dfd["fontFlagsWideCodes"]	 = $ff[5];
	$dfd["fontFlagsItalic"]		 = $ff[6];
	$dfd["fontFlagsBold"]		 = $ff[7];

	$ofst++;
	$dfd["languageCode"]		 = ui8dec($tagData[$ofst]);

	$ofst++;
	$dfd["fontNameLen"]			 = ui8dec($tagData[$ofst]);
	$ofst++;
	$fName = substr($tagData, $ofst, $dfd["fontNameLen"]);
	$z = NIL;

	if(strpos($fName, $z) !== false) $fName = substr($fName, 0, strlen($fName) - 1);

	$dfd["fontName"]			 = $fName;
	$ofst += $dfd["fontNameLen"];
	$dfd["numGlyphs"]			 = ui16dec(substr($tagData, $ofst, 2));
	$ofst += 2;
	$wo = 2;
	if($dfd["fontFlagsWideOffsets"]) $wo = 4;

	$oLength = $wo * $dfd["numGlyphs"];
	$dfd["offsetTable"] = substr($tagData, $ofst, $oLength);
	$ofst += $oLength;
	$cto = substr($tagData, $ofst, $wo);
	$dfd["codeTableOffset"] = $wo == 1 ? ui32dec($cto) : ui16dec($cto);
	$ofst += $wo;
	$dfd["dfBody"] = substr($tagData, $ofst);

	/*
	 * TODO: compute SHAPE[glyphShapeTable] to read from here and on

	...
	$dfd["glyphShapeTable"];

	$ofst += $gsTable["length"];
	...

	$dfd["codeTable"];
	$dfd["fontAscent"];
	$dfd["fontDescent"];
	$dfd["fontLeading"];
	$dfd["fontAdvanceTable"];
	$dfd["fontBoundsTable"];
	$dfd["kerningCount"];
	$dfd["fontKerningTable"];
	*/

	return $dfd;
}

/**
 * reads data
 * fill an associative array with defineFontAlignZones data
 */
function read_defineFontAlignZones_data($tagData) {

	$dfazd = read_tag_header_data(substr($tagData, 0, 10));
	$ofst = $dfazd["tagSL"] + 2;

	$cthStr = get_binData_string($tagData[$ofst]);

	$dfazd["csmTableHint"] = bindec(substr($cthStr, 0, 2));
	$ofst++;

	$dfazd["reserved"] = 0;
	$dfazd["zoneTable"] = array();

	//$ztData = substr($tagData, $ofst);
	//$ztLength = strlen($ztData);
	$tdLength = strlen($tagData);

	while($ofst < $tdLength){
		$zrd = read_zoneRecord_data(substr($tagData, $ofst));
		$dfazd["zoneTable"][] = $zrd;
		$ofst += $zrd["length"];
	}

	return $dfazd;
}

/**
 *
 */
function read_zoneRecord_data($buf){


	$zrd = array();
	$zrd["numZoneData"] = ui8dec($buf[0]);
	$ofst = 1;

	$zrd["zoneData"] = array();

	for($i = 0; $i < $zrd["numZoneData"]; $i++){
		$zrd["zoneData"][] = read_zoneData_data(substr($buf, $ofst, 4));
		$ofst += 4;
	}

	$zMask = get_binData_string($buf[$ofst]);
	$ofst++;

	$zrd["zoneMaskX"] = (int) $zMask[0];
	$zrd["zoneMaskY"] = (int) $zMask[1];
	$zrd["length"] = $ofst;

	return $zrd;
}

/**
 *
 */
function read_zoneData_data($buf){

	$zdd = array();
	$zdd["alignmentCoordinate"] = read_float16(substr($buf, 0, 2));
	$zdd["range"] = read_float16(substr($buf, 2, 2));

	return $zdd;
}


/**
 * reads data
 * return an array from a given tag data
 */
function read_csmTextSettings_data($tagData){

	$ctsd = read_tag_header_data(substr($tagData, 0, 10));
	$ofst = $ctsd["tagSL"] + 2;
	$ctsd["textID"] = $ctsd["itemID"];

	$cFlag = get_binData_string($tagData[$ofst]);
	$ofst++;

	$ctsd["useFlashType"] = bindec(substr($cFlag, 0, 2));
	$ctsd["gridFit"] 	  = bindec(substr($cFlag, 2, 3));
	// remaining 3 bit are always 0

	// FIXME: here we use fp 16.16 but we have to implement
	//        FLOAT32 (32bit single precision float)

	$thStr = get_binData_string(substr($tagData, $ofst, 2));
	$ctsd["thickness"] 	  = fp_binStr_float($thStr);

	$ofst += 2;
	$shStr = get_binData_string(substr($tagData, $ofst, 2));

	$ctsd["sharpness"]	  = fp_binStr_float($shStr);

	return $ctsd;
}

/**
 * reads data
 * return a DefineFontName array from a given tag data
 */
function read_fontName_data($tagData){

	$fnd = read_tag_header_data(substr($tagData, 0, 10));
	$ofst = $fnd["tagSL"] + 2;
	$fnd["fontID"] = $fnd["itemID"];
	$fnd["name"] = read_swf_string(substr($tagData, $ofst));
	$ofst += strlen($fnd["name"]) + 1;
	$fnd["copyright"] = read_swf_string(substr($tagData, $ofst));

	return $fnd;
}

//------------------------------------------------------------------------
// "WRITE" FUNCTIONS
//------------------------------------------------------------------------

/**
 * writes data
 * return defineEditText tag data froma a given associative array
 */
function write_textField_data($detd = array()){

	global $swfTagList;
	$tfBody = "";

	$tfBody .= swf_word($detd["itemID"]);
	$tfBody .= write_rect_data($detd["bounds"]["xmin"],
								$detd["bounds"]["xmax"],
								$detd["bounds"]["ymin"],
								$detd["bounds"]["ymax"]);

	$tffStr = $detd["hasText"]		 ? "1" : "0";
	$tffStr .= $detd["wordWrap"]	 ? "1" : "0";
	$tffStr .= $detd["multiline"]	 ? "1" : "0";
	$tffStr .= $detd["password"]	 ? "1" : "0";
	$tffStr .= $detd["readOnly"]	 ? "1" : "0";
	$tffStr .= $detd["hasTextColor"] ? "1" : "0";
	$tffStr .= $detd["hasMaxLength"] ? "1" : "0";
	$tffStr .= $detd["hasFont"]		 ? "1" : "0";
	$tffStr .= $detd["hasFontClass"] ? "1" : "0";
	$tffStr .= $detd["autoSize"]	 ? "1" : "0";
	$tffStr .= $detd["hasLayout"]	 ? "1" : "0";
	$tffStr .= $detd["noSelect"]	 ? "1" : "0";
	$tffStr .= $detd["border"]		 ? "1" : "0";
	$tffStr .= $detd["wasStatic"]	 ? "1" : "0";
	$tffStr .= $detd["html"]		 ? "1" : "0";
	$tffStr .= $detd["useOutlines"]	 ? "1" : "0";

	$tfBody .= get_binData($tffStr);

	if($detd["hasFont"]) {
		$tfBody .= swf_word($detd["fontID"]);
		$tfBody .= swf_word($detd["fontHeight"]);
	}

	if($detd["hasFontClass"]) {
		$tfBody .= swf_string($detd["fontClass"]);
	}

	if($detd["hasTextColor"])
		$tfBody .= write_color_data($detd["textColor"]);

	if($detd["hasMaxLength"])
		$tfBody .= swf_word($detd["maxLength"]);

	if($detd["hasLayout"]){

		$tfBody .= chr($detd["align"]);
		$tfBody .= swf_word($detd["leftMargin"]);
		$tfBody .= swf_word($detd["rightMargin"]);
		$tfBody .= swf_word($detd["indent"]);
		$tfBody .= swf_word($detd["leading"]);
	}

	//$tfBody .= NIL;

	if(strlen($detd["variableName"]))
		$tfBody .= swf_string($detd["variableName"]);
	else $tfBody .= NIL;

	if($detd["hasText"])
		$tfBody .= swf_string($detd["initialText"]);

	// note that this is one of many quirks in swf,
	// defineEditText (as long as images and other tags)
	// is always encapsulated in a "long tag" type
	$tfLength = strlen($tfBody);
	$tfHead = SWFTag_long_header($swfTagList["DefineEditText"], $tfLength);

	return $tfHead .$tfBody;
}

/**
 * writes data
 * compile defineFont2 or 3 tag data
 * from a given associative array
 */
function write_defineFont_data($fcd = array(), $flagLimit = false){

	/* TODO */

	global $swfTagList;
	$fcBody = "";

	$fcBody = swf_word($fcd["itemID"]);
	$flags = $fcd["fontFlagsHasLayout"]	   ? "1" : "0";
	$flags .= $fcd["fontFlagsShiftJIS"]	   ? "1" : "0";
	$flags .= $fcd["fontFlagsSmallText"]   ? "1" : "0";
	$flags .= $fcd["fontFlagsANSI"]		   ? "1" : "0";
	$flags .= $fcd["fontFlagsWideOffsets"] ? "1" : "0";
	$flags .= $fcd["fontFlagsWideCodes"]   ? "1" : "0";
	$flags .= $fcd["fontFlagsItalic"]	   ? "1" : "0";
	$flags .= $fcd["fontFlagsBold"]		   ? "1" : "0";

	$fcBody .= get_binData($flags);

	$fcBody .= chr(1);

	//$fcd["fontNameLen"]
	$fcBody .= chr(strlen($fcd["fontName"]));
	$fcBody .= $fcd["fontName"];
	$fcBody .= NIL;

	$fcBody .= swf_word($fcd["numGlyphs"]);

	if($fcd["fontFlagsWideCodes"]) $fcBody .= swf_word(2);

	// return only this part of tagData
	// if flagLimit is true
	// e.g. when updating only some flags
	// and having shape data already available
	if($flagLimit) return $fcBody;

	/* TODO */

	$fLength = strlen($fcBody);
	return SWFTag_long_header($swfTagList["DefineFont2"], $fLength) .$fcBody;
}

/**
 * writes data
 * compile DefineFontAlignZones tagData from a given array
 */
function write_defineFontAlignZones_data($dfazd = array()) {

	$tagBody = "";

	//return $tagData;
}

/**
 * writes data
 * compile ZONERECORD binary data from a given array
 */
function write_zoneRecord_data($zrd = array()){
	$buf = "";
	return $buf;
}

/**
 * writes data
 * compile ZONEDATA binary data from a given array
 */
function write_zoneData_data($zdd = array()){
	$buf = "";
	return $buf;
}

/**
 * writes data
 * compile CSMTextSettings tag data from a given array
 */
function write_csmTextSettings_data($ctsd = array()){

	$tagBody = "";

	//return;
}

/**
 * compile DefineFontName tag data
 */
function write_fontName_data($fnd = array()){

	global $swfTagList;

	$fnBody = swf_word($fnd["fontID"]);
	$fnBody .= swf_string($fnd["name"]);
	$fnBody .= swf_string($fnd["copyright"]);

	$fLength = strlen($fnBody);

	if($fLength < 63) return SWFTag_short_header($swfTagList["DefineFontName"], strlen($fnBody)) .$fnBody;
	else return SWFTag_long_header($swfTagList["DefineFontName"], strlen($fnBody)) .$fnBody;
}

//------end of mk lib text -----------------------------------------------

?>
